package com.dao;

import java.sql.*;

import com.bean.User;


public class UserDao implements Dao {
	
	private PreparedStatement stat = null;
	private Connection conn = null;
	private ResultSet rs = null; 
	
	
	
	public UserDao(){
		getConnection();
	}
	
	public void getConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zgy","root","root");
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("connection success!");
		
	}
	
	public User findUser(User u){
		
		String sql = "select * from user where name = ? and password = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setString(1, u.getUserName().trim());
			stat.setString(2, u.getPassword().trim());
			rs = stat.executeQuery();
			if(rs.next()){
				System.out.println(u);
				return u;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			closed();
		}
	
		return null;
	}
	
	public void closed(){
		
			try {
				if(rs!=null){
					rs.close();
					rs = null;
				}
				if(stat!=null){
					stat.close();
					stat = null;
				}
				if(conn!=null){
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
	}
}
